//
//  GlobalViewController.m
//  ShoppingExperience
//
//  Created by LiYinDi on 15/9/6.
//  Copyright (c) 2015年 RuiBen. All rights reserved.
//

#import "GlobalViewController.h"
#import "BasicViewController.h"
@interface GlobalViewController ()
{
     BOOL saveAccount;
}
//@property (nonatomic,strong) UIView *nBarView;
@end

@implementation GlobalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.nBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,44)];
//    [self.nBarView setBackgroundColor:[UIColor grayColor]];
//    [self.view addSubview:self.nBarView];
//    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    [button setTitle:@"返回" forState:UIControlStateNormal];
//    button.backgroundColor = [UIColor redColor];
//    
//    button.frame = CGRectMake(0, 0, 100, 44);
//    [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
//    
//    [self.nBarView addSubview:button];
   // NSLog(@"_noteStr====%@",_noteStr);
    if ([_ifShowNote isEqualToString:@"Y"]) {
        
     [Global showTextHudSub:_noteStr view:self.view];
    }
    _passwordTextField.secureTextEntry=YES;
//    
    _loginImageView.layer.borderColor=[[UIColor colorWithRed:239.0f/255.0f green:239.0f/255.0f blue:239.0f/255.0f alpha:1] CGColor];
    _loginImageView.layer.borderWidth= 1.0f;
    [_loginButton addTarget:self action:@selector(checkButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
    
    if ([userDefault objectForKey:@"SAVEACCOUNT"]) {
        [_loginImageView setImage:[UIImage imageNamed:@"btn_checked.png"] ];
        _businessAccountTextField.text=[userDefault objectForKey:@"BUSINESSNAME"];
        _userNameTextField.text=[userDefault objectForKey:@"USERNAME"];
        
        _passwordTextField.text=[userDefault objectForKey:@"PASSWORD"];
        saveAccount=[[userDefault objectForKey:@"SAVEACCOUNT"] boolValue];
        //    NSLog(@"saveAccount==%@",saveAccount=YES?YES:NO);
    }
    else
    {
        [_loginImageView setImage:nil];
        _userNameTextField.text=nil;
        _passwordTextField.text=nil;
        _businessAccountTextField.text=nil;
        saveAccount=[[userDefault objectForKey:@"SAVEACCOUNT"] boolValue];
    }


    // Do any additional setup after loading the view from its nib.
}
-(void)checkButtonClick
{
    saveAccount=!saveAccount;
    if (saveAccount) {
        [_loginImageView setImage:[UIImage imageNamed:@"btn_checked.png"]];
    }
    else
    {
        [_loginImageView setImage:nil];
    }
    
    
}
- (IBAction)loginButtonClick:(UIButton *)sender {
    
    [Global showIndeterminateHudSub:@"登录中，请稍后。。。" delay:30 view:self.view];
    NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
    [params setObject:_userNameTextField.text forKey:@"UserName"];
    [params setObject:_businessAccountTextField.text forKey:@"CompanyCode"];
    [params setObject:[Global md5:_passwordTextField.text] forKey:@"LoginWord"];
    [params setObject:@"S" forKey:@"AppType"];
    // NSLog(@"parr===%@",params);
    [HttpRequest requestLoginData:@"System/Login" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            //   NSLog(@"severddd===%@",severDataDict.dataDict);
            // [Global hideProgressHUDInternal:self.view];
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                NSString *tokenstr=[severDataDict.dataDict objectForKey:@"TokenID"];
                [Global setLoginUrl:[severDataDict.dataDict objectForKey:@"AppUrl"]];
                [Global setLoginToken:tokenstr];
                [Global setGuideCodeStr:[severDataDict.dataDict objectForKey:@"UserCode"]];
                [Global setGuideNameStr:[severDataDict.dataDict objectForKey:@"UserName"]];
                
                 [Global setStoreName:[severDataDict.dataDict objectForKey:@"StoreName"]];
              
                  NSString *iconStr=[severDataDict.dataDict objectForKey:@"ImageUrl"];
                 [Global setHeadIcon:iconStr];
                
                // [Global setPhoneNumber:_userNameTextField.text];
                // [self requestStoreDetail:tokenstr];
                if (saveAccount) {
                    [[NSUserDefaults standardUserDefaults] setObject:_businessAccountTextField.text forKey:@"BUSINESSNAME"];
                    [[NSUserDefaults standardUserDefaults] setObject:_userNameTextField.text forKey:@"USERNAME"];
                   
                    [[NSUserDefaults standardUserDefaults] setObject:_passwordTextField.text forKey:@"PASSWORD"];
                    [[NSUserDefaults standardUserDefaults] setBool:saveAccount forKey:@"SAVEACCOUNT"];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                    
                }
                else
                {
                    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"USERNAME"];
                    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"PASSWORD"];
                    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"SAVEACCOUNT"];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                }
                if ([_ifShowNote isEqualToString:@"Y"]) {
                    
               
                 [self.navigationController popViewControllerAnimated:YES];
                }
                else
                {
                    BasicViewController *controller=[[BasicViewController alloc]init];
                   
                    [self.navigationController pushViewController:controller animated:NO];
                }
                
            }
            else
            {
                // NSLog(@"loginnnnnnnnn");
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                // [Global showTextHud:requestResultStr view:self.view];
                [Global showTextHudSub:requestResultStr view:self.view];
            }
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:@"连接失败" view:self.view];
            
        }
    }];
    

}
-(IBAction)clearBusinessTextClick:(id)sender
{
 _businessAccountTextField.text=@"";
}
-(IBAction)clearAccountTextClick:(id)sender
{
    _userNameTextField.text=@"";
}
-(IBAction)setPasswordVisible:(id)sender
{
    _passwordTextField.secureTextEntry=!_passwordTextField.secureTextEntry;
    if (_passwordTextField.secureTextEntry) {
        [_visibleButton setImage:[UIImage imageNamed:@"yanjing_normal.png"] forState:UIControlStateNormal];
    }
    else
        [_visibleButton setImage:[UIImage imageNamed:@"yanjing_hight.png"] forState:UIControlStateNormal];
    
    
    
}


//- (IBAction)backClick:(UIButton *)sender {
//    [self.navigationController popViewControllerAnimated:YES];
//}

//-(void)backAction:(id)sender
//{
//    [self.navigationController popViewControllerAnimated:YES];
//    
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
